package com.soho.mdi;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;

import javax.swing.JDesktopPane;

public class MyDesktop extends JDesktopPane {
	private Point point;
	private ArrayList compList;
	private final MyDesktop desktop;
	public MyDesktop(){
		super();
		point = new Point();
		desktop = this;
		compList = new ArrayList();
		point.x = 0;
		point.y = 0;
		
    	this.addComponentListener(new ComponentAdapter() {
    	    public void componentResized(ComponentEvent e) {
    	    	point.x = 0;
    	    	point.y = 0;

    	    	for(int i=0;i<compList.size();i++){
    	    		Component c = (Component)compList.get(i);
    	    		if(point.y+c.getHeight()>=e.getComponent().getHeight()){
    	    			point.y = 0;
    	    			point.x += c.getWidth()+10;
    	    		}    	    		
    	    		c.setLocation(point);
    	    		point.y += c.getHeight()+10;    	    	
    	    	}
    	    }
    	});		
	}
	
	
	public Component addIcon(Component object){	
		if(point.y+object.getHeight()>=this.getHeight()){
			point.y = 0;
			point.x += object.getWidth()+10;
		}
		compList.add(object);
		object.setLocation(point);
		point.y += object.getHeight()+10;
		
		return super.add(object);
	}
	
	
	

}
